<template>
	<view class="login-container">
	<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
	<button type="primary" class="btn-login"  @click="getUserProfile">一键登录</button>
	<view class="login-text">登录享用更多权益</view>
	</view>
</template>

<script>
	import {mapState,mapMutations} from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				// userInfo:{}
			};
		},
		computed:{
			...mapState('m_user',['userInfo'])
		},
		methods:{
			...mapMutations('m_user',['updateUserInfo','updateToken']),
			// 调用登录api获取token
		async getToken(info){
				// 调用微信登录接口
			const [err, res] = await uni.login().catch(err => err)
		if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败！')
		// 准备参数对象
		  const query = {
		    code: res.code,
		    encryptedData: info.encryptedData,
		    iv: info.iv,
		    rawData: info.rawData,
		    signature: info.signature
		  }
		  // console.log(query)
		   // 换取 token
		    const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
			// console.log(loginResult)
		    if (loginResult.meta.status !== 200) return uni.$showMge('登录失败！使用假的token')
		     // 2. 更新 vuex 中的 token
		      this.updateToken(loginResult.message.token)
			//   this.updateToken('sdjdhbjs')
			},
			getUserProfile(e){
				uni.getUserProfile({
					desc:'用于登录',
					success: (res) => {
						// this.userInfo = res.userInfo
						this.updateUserInfo(res.userInfo)
						// console.log(res)
						this.getToken(res)
					},
					 
				})
			}
		}
	}
</script>

<style lang="scss">
.login-container{
	height: 750rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	background-color: #f8f8f8;
	position: relative;
	overflow:hidden;
	&::after{
		content: '';
		display: block;
		width: 100%;
		height: 40px;
		position: absolute;
		top: 0;
		bottom: 0;
		border-radius: 100%;
		transform: translateY(50%);
	}
}
.btn-login{
	width: 90%;
	margin: 15px 0;
	border-radius: 100px;
	background-color: #c00000;
}
.login-text{
	font-size: 12px;
	color: gray;
}
</style>
